using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MP_Syndication.Configuration
{
    public partial class FeedForm : Form
    {
        private SyndicationDatabase _syndicationDB;
        private ConfigurationForm _configurationForm;

        public FeedForm(SyndicationDatabase syndicationDB, ConfigurationForm configurationForm)
        {
            _syndicationDB = syndicationDB;
            _configurationForm = configurationForm;
            InitializeComponent();
        }

        /// <summary>
        /// Show openFileDialog to select a image for this feed. The image 
        /// is used if no image is specified in the given feed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btBrowse_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
        }

        /// <summary>
        /// Save the selected imagePath to the textField and show the 
        /// preview image in the upper right corner
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            tbImagePath.Text = openFileDialog1.FileName;
            feedImage.ImageLocation = openFileDialog1.FileName;
            feedImage.Visible = true;
        }

        /// <summary>
        /// Updates the property label on screen to show what
        /// property should be used in the skin files to use 
        /// the ticker
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbProperty_TextChanged(object sender, EventArgs e)
        {
            lbProperty.Text = tbProperty.Text;
        }

        /// <summary>
        /// Cancel changes
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        /// <summary>
        /// Saves the form input to the database if valid
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btSave_Click(object sender, EventArgs e)
        {
            if (validateFormInput())
            {
                Feed inputFeed = new Feed(tbFeedName.Text, tbFeedURL.Text,(int) nbItems.Value, tbImagePath.Text,
                                        cbTickerEnabled.Checked, tbSeparator.Text, tbProperty.Text,
                                        cbAutorefreshEnabled.Checked, (int)nbRefreshInterval.Value);
                _syndicationDB.addFeed(ref inputFeed);
                _configurationForm.addFeed(inputFeed);
            }
            this.Dispose();
        }

        /// <summary>
        /// Validates the user input
        /// </summary>
        /// <returns>true if the form input is valid</returns>
        private bool validateFormInput()
        {
            if (tbFeedName.Text.Equals(String.Empty) || tbFeedName.Text.Length == 0)
                return false;

            if (tbFeedURL.Text.Equals(String.Empty) || tbFeedURL.Text.Length == 0)
                return false;

            if (cbTickerEnabled.Checked)
            {
                if (tbProperty.Text.Equals(String.Empty) || tbProperty.Text.Length == 0)
                    return false;

            }

            // No failures so return true
            return true;
        }


    }
}